package com.zjht.gmcc.servlet.sys;

/**
 * 2010-12-30 
 * 朱家进
 * 去掉opercode不可重复问题，增加operid
 */
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjht.gmcc.bean.BaseServlet;
import com.zjht.gmcc.bean.util.MyCode;
import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyParamUtil;


public class OperAdd extends BaseServlet {

	private static final long serialVersionUID = 1L;


	public boolean process(HttpServletRequest request, HttpServletResponse response,HttpSession session, PrintWriter out, MyJDBC myJDBC,  String menu)throws Exception {
		int i_flag = 0;

		// ===============
		String s_orgacode = request.getParameter("orgacode");
		String s_username = request.getParameter("username");
		String s_usercode = request.getParameter("usercode").trim().toUpperCase();
		String s_deptid_dutyid = request.getParameter("deptid_dutyid");
		String[] ssarr = s_deptid_dutyid.split("#");
		String s_deptid = ssarr[0];
		String s_dutyid = ssarr[1];
		String s_tel = request.getParameter("tel");
		String s_email = request.getParameter("email");
		String s_pass = request.getParameter("pass1");
		String orgid = null;

		String s_status = (request.getParameter("status") == null) ? "0" : request
				.getParameter("status");

		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String s_sql = "SELECT orgid FROM organiza WHERE orgacode = ?";
		ps = myJDBC.prepareStatement(s_sql);
		ps.setString(1, s_orgacode);
		rs = ps.executeQuery();
		if (rs.next()) orgid = rs.getString("orgid");
		rs.close();
		rs = null;
		ps.close();
		
		String operid = String.valueOf(MyParamUtil.getSEQ("seq_operid"));	
		String s_cshpassword = MyCode.MD5(s_pass);
		s_sql = "INSERT INTO opers(opercode, opername, passwd, orgacode, deptid, email, "
				+ "telephone, createdate, status, opertype, showflag,orgid,operid) "
				+ "VALUES(?,?,?,?,?,?,?,sysdate,?,?,?,?,?)";
		ps = myJDBC.prepareStatement(s_sql);
		ps.setString(1, s_usercode);
		ps.setString(2, s_username);
		ps.setString(3, s_cshpassword);
		ps.setString(4, s_orgacode);
		ps.setString(5, s_deptid);
		ps.setString(6, s_email);
		ps.setString(7, s_tel);
		ps.setString(8, s_status);
		ps.setString(9, "1");// 1为普通用户，2为航天电子用户
		ps.setString(10, s_status);// 1为显示，0为隐藏
		ps.setString(11, orgid);
		ps.setString(12, operid);
		i_flag = ps.executeUpdate();
		s_sql = null;
		ps.close();
		ps = null;
		if (i_flag == 1) {
			s_sql = "INSERT INTO operspopedom(opercode,dutyid,operid) VALUES(?,?,?)";
			ps = myJDBC.prepareStatement(s_sql);
			ps.setString(1, s_usercode);
			ps.setString(2, s_dutyid);
			ps.setString(3, operid);
			i_flag = ps.executeUpdate();
			s_sql = null;
			ps.close();
			ps = null;
		}

		String act = "用户添加";
		if (i_flag > 0) {
			alertAndReturnValue(out,act + "成功", "1");
//			com.zjht.gmcc.bean.sms.Task.UpdateSMSReceiver(s_tel, "", s_dutyid, s_orgacode,
//					s_usercode, s_username);
		} else {
			errAlert(request, out, menu, act + "失败");
		}
		return i_flag > 0;

	}
}
